using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.TestDevice.Manager.Interface.Services;
using Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Views;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Constants;
using Jbe.TestSuite.TestDevice.FunctionGen.Interface.Services;
using Microsoft.Practices.CompositeUI.Commands;
using Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Properties;
using SCSFContrib.CompositeUI.WinForms.SmartPartInfos;

namespace Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Services
{
    public class FGenControlView : IControlView
    {
        private WorkItem workItem;
        private IDevice device;
        private string name;
        private FunctionGenView functionGenView;

        [ServiceDependency]
        public WorkItem WorkItem
        {
            set { workItem = value; }
        }

        [ServiceDependency]
        public IDevice Device
        {
            get { return device; }
            set 
            { 
                device = value;
                device.Disconnected += new EventHandler(DeviceDisconnected);
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public void Show()
        {
            if (functionGenView == null)
            {
                DockPanelSmartPartInfo dpInfo = new DockPanelSmartPartInfo(Name, device.ProductName);
                dpInfo.Icon = Resources.Device;
                functionGenView = workItem.SmartParts.AddNew<FunctionGenView>();
                functionGenView.Disposed += new EventHandler(FunctionGenViewDisposed);
                
                workItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(functionGenView, dpInfo);
            }
            workItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Activate(functionGenView);
        }

        public void Close()
        {
            if (functionGenView != null)
            {
                workItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Close(functionGenView);
            }

            device.Disconnected -= DeviceDisconnected;
            device.Disconnect();
        }

        #region Command and Event handler

        private void FunctionGenViewDisposed(object sender, EventArgs e)
        {
            workItem.SmartParts.Remove(functionGenView);
            functionGenView = null;
        }

        private void DeviceDisconnected(object sender, EventArgs e)
        {
            Close();
        }

        [CommandHandler(CommandNames.ConfigureFunctionGen)]
        public void ConfigureFunctionGen(object sender, EventArgs e)
        {
            ICollection<ConfigurationEdit> c = workItem.SmartParts.FindByType<ConfigurationEdit>();

            FormSmartPartInfo spi = FormSmartPartInfo.CreateModalDialog(Name, device.ProductName);
            ConfigurationEdit configEdit = workItem.SmartParts.AddNew<ConfigurationEdit>();
            spi.AcceptButton = configEdit.AcceptButton;
            spi.CancelButton = configEdit.CancelButton;
            configEdit.Disposed += new EventHandler(ConfigEditDisposed);
            workItem.Workspaces[WorkspaceNames.ModalWindows].Show(configEdit, spi);
        }

        private void ConfigEditDisposed(object sender, EventArgs e)
        {
            workItem.SmartParts.Remove(sender);
        }

        #endregion
    }
}
